<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="车牌号" prop="carNum">
        <el-input
          v-model="queryParams.carNum"
          placeholder="请输入车牌号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车架号" prop="vinNum">
        <el-input
          v-model="queryParams.vinNum"
          placeholder="请输入车辆识别代号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发动机号" prop="engineNum">
        <el-input
          v-model="queryParams.engineNum"
          placeholder="请输入发动机/电动机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册日期" prop="regDate">
        <el-input
          v-model="queryParams.regDate"
          placeholder="请输入注册日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发证日期" prop="issueDate">
        <el-input
          v-model="queryParams.issueDate"
          placeholder="请输入发证日期"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用性质 " prop="useType">
        <el-select v-model="queryParams.useType" placeholder="请选择使用性质 " clearable size="small">
          <el-option
            v-for="dict in useTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="燃油类型  " prop="fuelType">
        <el-select v-model="queryParams.fuelType" placeholder="请选择燃油类型  " clearable size="small">
          <el-option
            v-for="dict in fuelTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="事故" prop="hasAccident">
        <el-select v-model="queryParams.hasAccident" placeholder="请输入是否有人伤事故  " clearable size="small">
          <el-option
            v-for="dict in hasAccidentOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="座位数" prop="seat">
        <el-input
          v-model="queryParams.seat"
          placeholder="请输入座位数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['member:car:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['member:car:remove']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="carList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车牌号" align="center" prop="carNum" />
      <el-table-column label="车辆类型" align="center" prop="carType" />
      <el-table-column label="所有人" align="center" prop="carOwner" />
      <el-table-column label="品牌型号" align="center" prop="brandModel" />
      <el-table-column label="车架号" align="center" prop="vinNum" />
      <el-table-column label="发动机号" align="center" prop="engineNum" />
      <el-table-column label="注册日期" align="center" prop="regDate" />
      <el-table-column label="发证日期" align="center" prop="issueDate" />
      <el-table-column label="使用性质 " align="center" prop="useType" :formatter="useTypeFormat" />
      <el-table-column label="燃油类型  " align="center" prop="fuelType" :formatter="fuelTypeFormat" />
      <el-table-column label="事故" align="center" prop="hasAccident" :formatter="hasAccidentFormat" />
      <el-table-column label="座位数" align="center" prop="seat" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogdrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="车牌号" prop="carNum">
          <el-input v-model="form.carNum" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="车辆类型">
            <el-input v-model="form.carType" placeholder="请选择车辆类型" />
        </el-form-item>
        <el-form-item label="所有人" prop="carOwner">
          <el-input v-model="form.carOwner" placeholder="请输入所有人" />
        </el-form-item>
        <el-form-item label="品牌型号" prop="brandModel">
          <el-input v-model="form.brandModel" placeholder="请输入品牌型号" />
        </el-form-item>
        <el-form-item label="车架号" prop="vinNum">
          <el-input v-model="form.vinNum" placeholder="请输入车辆识别代号" />
        </el-form-item>
        <el-form-item label="发动机号" prop="engineNum">
          <el-input v-model="form.engineNum" placeholder="请输入发动机/电动机号码" />
        </el-form-item>
        <el-form-item label="注册日期" prop="regDate">
          <el-input v-model="form.regDate" placeholder="请输入注册日期" />
        </el-form-item>
        <el-form-item label="发证日期" prop="issueDate">
          <el-input v-model="form.issueDate" placeholder="请输入发证日期" />
        </el-form-item>
        <el-form-item label="使用性质 ">
          <el-select v-model="form.useType" placeholder="请选择使用性质 ">
            <el-option
              v-for="dict in useTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="燃油类型  ">
          <el-select v-model="form.fuelType" placeholder="请选择燃油类型  ">
            <el-option
              v-for="dict in fuelTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="人伤事故" prop="hasAccident">
          <el-select v-model="form.hasAccident" placeholder="请输入是否有人伤事故  " clearable size="small">
            <el-option
              v-for="dict in hasAccidentOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="座位数" prop="seat">
          <el-input v-model="form.seat" placeholder="请输入座位数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCar, getCar, delCar, addCar, updateCar, exportCar } from "@/api/member/car";

export default {
  name: "Car",
  props: {
    memberId: {
      type: Number
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 用户车辆表格数据
      carList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 使用性质 字典
      useTypeOptions: [],
      // 燃油类型  字典
      fuelTypeOptions: [],
      // 是否有人伤事故  0 无  1有字典
      hasAccidentOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: this.memberId,
        carNum: undefined,
        carType: undefined,
        carOwner: undefined,
        brandModel: undefined,
        vinNum: undefined,
        engineNum: undefined,
        regDate: undefined,
        issueDate: undefined,
        useType: undefined,
        fuelType: undefined,
        hasAccident: undefined,
        seat: undefined,
        gmtTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("car_use_type").then(response => {
      this.useTypeOptions = response.data;
    });
    this.getDicts("car_fuel_type").then(response => {
      this.fuelTypeOptions = response.data;
    });
    this.getDicts("sys_is_enable").then(response => {
      this.hasAccidentOptions = response.data;
    });
  },
  methods: {
    /** 查询用户车辆列表 */
    getList() {
      this.loading = true;
      listCar(this.queryParams).then(response => {
        this.carList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 使用性质 字典翻译
    useTypeFormat(row, column) {
      return this.selectDictLabel(this.useTypeOptions, row.useType);
    },
    // 燃油类型  字典翻译
    fuelTypeFormat(row, column) {
      return this.selectDictLabel(this.fuelTypeOptions, row.fuelType);
    },
    // 是否有人伤事故  0 无  1有字典翻译
    hasAccidentFormat(row, column) {
      return this.selectDictLabel(this.hasAccidentOptions, row.hasAccident);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        carNum: undefined,
        carType: undefined,
        carOwner: undefined,
        brandModel: undefined,
        vinNum: undefined,
        engineNum: undefined,
        regDate: undefined,
        issueDate: undefined,
        useType: undefined,
        fuelType: undefined,
        hasAccident: undefined,
        seat: undefined,
        gmtTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCar(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户车辆";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateCar(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addCar(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户车辆编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCar(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户车辆数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCar(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
